from pydantic import BaseModel, field_validator, model_validator, validator
from typing import Type

def convert_id(cls: Type[BaseModel]):
    
    # 遍历类的字段
    for field_name, field_type in cls.__annotations__.items():
        # 如果字段名以 "_id" 结尾
        if field_name.endswith("_id"):
            # 为符合条件的字段添加一个 validator
            method_name = f"convert_{field_name}"
            print(method_name)
            attr_ = field_name
            # 动态添加验证方法
            @field_validator(field_name)
            def convert_to_str(cls, field_type):
                # 转换为字符串，如果是 None 则赋默认值 "-1"
                print(f"-----------------------------{method_name}|{field_type}-----------------------------")
                print(f"-----------------------------|{str(field_type) if field_type is not None else '-1'}|-----------------------------")
                return str(field_type) if field_type is not None else "-1"

            # 将验证方法绑定到类
            setattr(cls, method_name, convert_to_str)
    for field_name, field_type in cls.__bases__[0].__annotations__.items():
        if field_name.endswith("_id"):
            # 为符合条件的字段添加一个 validator
            method_name = f"convert_{field_name}"
            print(method_name)
            attr_ = field_name
            # 动态添加验证方法
            @field_validator(field_name)
            def convert_to_str(cls, field_type):
                # 转换为字符串，如果是 None 则赋默认值 "-1"
                print(f"-----------------------------{method_name}|{field_type}-----------------------------")
                print(f"-----------------------------|{str(field_type) if field_type is not None else '-1'}|-----------------------------")
                return str(field_type) if field_type is not None else "-1"

            # 将验证方法绑定到类
            setattr(cls, method_name, convert_to_str)
    return cls

def create_validator(field_name: str):
    """
    创建一个验证器函数，用于将值转换为字符串，若值为 None，则设为 "-1"。
    """
    def validator_func(cls, value):
        return str(value) if value is not None else "-1"
    return validator_func

def convert_fields(fields: list[str]):
    def decorator(cls):
        print(cls.__bases__)
        print(cls.__bases__[0].__annotations__)
        # 为指定的字段添加 validator
        for field_name in fields:
            print(f"-----------------------------{field_name}-----------------------------")
            print(hasattr(cls, field_name))
            method_name = f"convert_{field_name}_to_str"
            print(f"-----------------------------{method_name}-----------------------------")
            @validator(field_name, pre=True)
            def convert_to_str(cls, value):
                # 转换为字符串，如果是 None 则赋默认值 "-1"
                return str(value) if value is not None else "-1"

            # 将验证方法绑定到类
            setattr(cls, method_name, convert_to_str)
            if hasattr(cls, field_name):  # 确保类中有这个字段
                pass
        return cls
    return decorator

# 使用装饰器
# @convert_id
class UserRoleBase(BaseModel):
    user_id: str | int | None = None
    role_id: str | int | None = None


# 继承 UserRoleBase
# @convert_fields(['user_id', 'role_id'])
@convert_id
class UserRole(UserRoleBase):
    # user_id: str | None = None
    # role_id: str | None = None
    class Config:
        from_attributes = True

# 测试
user_role = UserRole(user_id=123, role_id=None)
print(user_role.user_id)  # 输出 "123"
print(user_role.role_id)  # 输出 "-1"


